﻿using System;
using System.Collections.Generic;
using System.Text;
using Slippor.Common.PluginFramework;

namespace AutoFavHandlerPlugin
{
    public class AutoFavConfig : IConfig
    {
        #region IConfig Members

        ConfigForm _configForm;
        public bool Config(ref IPlugin plugin, out object config)
        {
            config = _interval;
            if (_configForm != null && !_configForm.IsDisposed)
            {
                _configForm.Close();
            }
            _configForm = new ConfigForm(this);
            if( _configForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                config = _interval;
                return true;
            }
            return false;
        }

        public static readonly TimeSpan DefaultInterval = new TimeSpan(0, 0, 3, 0);
        private TimeSpan _interval = DefaultInterval;
        public object ConfigSetting
        {
            get
            {
                return _interval.ToString();
            }
            set
            {
                TimeSpan timeSpan;
                TimeSpan.TryParse(value.ToString(), out timeSpan);
                _interval = timeSpan;
            }
        }

        public void Update(ref IPlugin plugin, ref object config)
        {
            ConfigSetting = config;
            AutoFavHandlerPlugin autoFavPlugin = plugin as AutoFavHandlerPlugin;
            autoFavPlugin.SetInterval(_interval);
        }

        public void Reset(ref IPlugin plugin)
        {
            _interval = DefaultInterval;
            AutoFavHandlerPlugin autoFavPlugin = plugin as AutoFavHandlerPlugin;
            autoFavPlugin.SetInterval(_interval); ;
        }
        #endregion
    }
}
